<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>工作流管理系统</title>
    <head th:replace="source::header"></head>
    <script type="application/javascript" th:src="@{/static/lib/encrypt-min.js}"></script>
    <script type="application/javascript" th:src="@{/static/lib/encrypt-min.js}"></script>
    <style type="text/css">
        body {
            height: 100%;
            position: relative;
        }

        body > div {
            width: 100%;
            position: absolute;
        }

        #header {
            height: 70px;
            z-index: 2;
            background: #0986D6;
            text-align: center;
        }

        #header > div {
            widht: 1024px;

        }

        #header > div h1 {
            line-height: 70px;
            color: #fff;
            font-size: 18px;
        }

        #main_body {
            z-index: 1;
            border-top: 70px solid #fff;
            border-bottom: 100px solid #fff;
            width: 1024px;
            margin: 0 auto;
            height: 100%;
            position: relative;
        }

        #footer {
            height: 100px;
            z-index: 2;
            bottom: 0;
        }

        #footer p {
            line-height: 32px;
            text-align: center;
        }

        #footer p a {
            padding: 0 20px;
        }

        #content {
            width: 1024px;
            height: 360px;
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
            margin: auto;
        }

        #content > div {
            height: 100%;
        }

        #login {
            width: 370px;
            position: absolute;
            top:0;
            right:0;
            border: 7px solid #DBEDFF;
            padding: 22px;
            background: #FFFFFF;
        }

        .title {
            font-size: 18px;
            height: 30px;
            line-height: 30px;
            padding-left: 20px;
            border-left: 10px solid #0986d6;
            color: #333333;
        }

        .title cite {
            color: #C4C4C4;
            margin-left: 15px;
            font-weight: 400;
        }

        .add-box {
            margin-top: 25px;
        }

        .add-box .add-con {
            height: 38px;
            margin-bottom: 25px;
            position: relative;
        }

        .add-box a.loginn {
            border-radius: 4px;
            background: #0e89cc;
            color: #FFFFFF;
            width: 100%;
            height: 38px;
            line-height: 38px;
            text-align: center;
            font-size: 16px;
            display: block;
        }

        .add-box .add-con i {
            font-size: 20px;
            color: #666666;
            position: absolute;
            top: 10px;
            left: 10px;
            border-right: 1px solid #E6E6E6;
            padding-right: 13px;
        }

        .code .change {
            color: #0044CC;
        }

        .text {
            display: block;
            position: absolute;
            top: 6px;
            right: 0px;
            font-size: 14px;
            text-align: right;
            cursor: pointer;
        }

        .w100 {
            width: 100px !important;
        }

        .code_img {
            display: inline-block;
            position: absolute;
            top: 6px;
            left: 170px;
        }

        .add-box .add-con input {
            width: 260px;
            height: 38px;
            line-height: 38px;
            outline: none !important;
            font-size: 16px;
            padding-left: 50px;
            color: #333333;
            border: 1px solid #E6E6E6;
        }
    </style>
</head>
<body>
<div id="header">
    <div>
        <h1 style="letter-spacing:3px;">工作流管理系统</h1>
    </div>
</div>
<div id="main_body" class="k_box_size">
    <div id="content">
        <div id="logo"></div>
        <div id="login" class="k_box_size">
            <div class="title">
                用户登录 <cite>LOGIN</cite>
            </div>
            <div class="add-box">
                <div class="add-con">
                    <i class="fa fa-users" style="padding-right:15px"></i>
                    <input type="text" id="userName"/>
                </div>
                <div class="add-con">
                    <i class="fa fa-lock-open-alt"  style="padding-right:27px"></i>
                    <input type="password" id="userPwd">
                </div>
                <div class="add-con code">
                    <i class="fa fa-file-image"  style="padding-right:22px"></i>
                    <input type="text" style="width:130px" id="imgCode">
                    <div class="code_img">
                        <img id="img_code">
                    </div>
                    <div class="text">
                        <p id="makeImg">看不清？</p>
                    </div>
                </div>
                <a href="#" id="login_btn" class="loginn"> 登录 </a>
                <p id="message" style="line-height:30px;color:red;text-align:center;margin-top:5px;"></p>
            </div>
        </div>
    </div>
</div>
<div id="footer">
    <!-- <p>
        <a>联系我们</a><a>关于平台</a><a>平台帮助</a>
    </p> -->
    <p>Copyright © 2019 - [(${year})] 工作流管理系统</p>
</div>
<script type="application/javascript" th:inline="javascript">
    var $imgCode,
        imgKey,
        $message,
        isLoging,
       $pwd, $uName;

    function getImgCode() {
        $B.request({
            url: $B.getHttpHost(ctxPath) + "v1/getCode",
            ok: function (key, imgData) {
                $imgCode.src = imgData;
                imgKey = key;
            }
        });
    }

    function pageLoaded() {
        console.log($B);

        var AESKey;
        var keyPair = $B.RSAGenKeys();
        console.log(keyPair);
        let publicKey = keyPair.publicKey;
        let privateKey = keyPair.privateKey;
        let cipherKey = $B.RSAEncrypt(publicKey,_PUBLICKEY);
        console.log(_PUBLICKEY);
        console.log("cipherKey="+cipherKey);
        $B.request({
            waiting:true,
            url: $B.getHttpHost(ctxPath) + "v1/aeskey",
            data: {
                publicKey:cipherKey,
                _not_url_decode_: '1',
            },
            ok: function (msg, data) {
                console.log(msg,data);
                AESKey =  $B.RSADecrypt(data,privateKey);
                console.log("AESKey ="+AESKey);
            }
        });



        $message = document.getElementById("message");
        $imgCode = document.getElementById("img_code");
        getImgCode();
        var $imgBtn = document.getElementById("makeImg");
        $B.DomUtils.click($imgBtn, function () {
            getImgCode();
        });
        var lBtn = document.getElementById("login_btn");
        $B.DomUtils.click(lBtn, function () {
            if(isLoging){
                return;
            }
            if(!AESKey){
                $B.error("AESKey 为空，请刷新重试！",1.5);
                return;
            }
            var $btn = this;
            $pwd = document.getElementById("userPwd");
            $uName = document.getElementById("userName");
            if($pwd.value == "" || $uName.value === ""){
                $message.innerText = "用户名、密码不能为空！";
                $B.DomUtils.addClass($pwd,"k_input_value_err");
                $B.DomUtils.addClass($uName,"k_input_value_err");
                return;
            }
            $B.DomUtils.removeClass($pwd,"k_input_value_err");
            $B.DomUtils.removeClass($uName,"k_input_value_err");
            $B.DomUtils.attribute($btn, {"disabled": true});
            $btn.innerText = "正在登录...";
            $message.innerText = "";
            var $icode = document.getElementById("imgCode");
            var icode = $icode.value;
            var userPwd = $B.AESEncrypt($pwd.value,AESKey);
            console.log("userPwd " + userPwd);
            isLoging = true;
            $B.request({
                url: $B.getHttpHost(ctxPath) + "v1/login",
                //contentType:'application/json',
                data: {
                    userName: $uName.value,
                    userPwd: userPwd,
                    imgKey: imgKey,
                    _not_url_decode_: '1',
                    imgCode: icode
                },
                ok: function (msg, data) {
                    $B.DomUtils.attribute($btn, {"disabled": true});
                    $btn.innerText = "正在进入系统...";
                    window.location.href = $B.getHttpHost(ctxPath) + "v1/page/index";
                },
                fail: function (message, res) {
                    imgKey = res.data.imgKey;
                    $imgCode.src = res.data.img;
                    $message.innerHTML = message;
                    $B.DomUtils.removeAttribute($btn, "disabled");
                    $btn.innerText = "登录";
                    isLoging = false;
                }
            });

        });
        $B.DomUtils.keyup(document, function (event) {
            if (event.keyCode == "13") {
                 $B.DomUtils.trigger(lBtn,"click");
            }
        });
        if (window.top !== window.self) {
            window.top.location.href = $B.getHttpHost(ctxPath);
        }
        $B.bindInputClear(document.getElementById("login"));
    }

    $B.DomUtils.onDomLoaded(function () {
        pageLoaded();
    });
</script>
</body>
</html>